﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using Ue.Qcdz.Domain.ResourceManager;

namespace Ue.Qcdz.Domain
{
    public class Email
    {
        public static void SendMail(string sendEmail, string titleInnerHtml, string bodyInnerHtml)
        {
           
            string smtp = ResourceGlobals.Instance.GetString("Mail_Smtp");
            string fromEmail = ResourceGlobals.Instance.GetString("Mail_FromEmail");
            string emailUser = ResourceGlobals.Instance.GetString("Mail_EmailUser");
            string emailPsw = ResourceGlobals.Instance.GetString("Mail_EmailPsw");

            if (string.IsNullOrEmpty(smtp) || string.IsNullOrEmpty(sendEmail) || string.IsNullOrEmpty(emailUser) || string.IsNullOrEmpty(emailPsw))
                throw new Exception("邮件服务器配置错误，发送的邮件信息不完整。");

            MailMessage msg = new MailMessage();
            msg.To.Add(sendEmail);
            msg.From = new MailAddress(fromEmail);
            msg.Subject = titleInnerHtml;//邮件标题 
            msg.SubjectEncoding = System.Text.Encoding.GetEncoding("GB2312");//邮件标题编码 
            msg.Body = bodyInnerHtml;//邮件内容 
            msg.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");//邮件内容编码 
            msg.IsBodyHtml = true;//是否是HTML邮件 
            msg.Priority = MailPriority.High;//邮件优先级 

            SmtpClient client = new SmtpClient(smtp);
            client.Credentials = new NetworkCredential(emailUser, emailPsw);

            try
            {
                client.Send(msg);
            }
            catch (Exception exception)
            {
                throw new Exception(exception.Message);
            }
        }
    }
}
